package com.example.njut;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyInfoActivity extends AppCompatActivity {
    private EditText uname;
    private EditText pwd;
    private EditText age;
    private Button showPwd;
    private Button update;
    private Button save;

    private int i = 0;


    dbHelper dbHelper;
    String DB_Name="mydb";
    SQLiteDatabase db;
    Cursor cursor;

    boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);

        //创建连接，并打开数据库
        dbHelper = new dbHelper(this,DB_Name,null,1);
        db = dbHelper.getWritableDatabase();

        uname = (EditText) findViewById(R.id.text1);
        pwd = (EditText) findViewById(R.id.text2);
        showPwd = (Button) findViewById(R.id.b1);
        age = (EditText) findViewById(R.id.text3);
        update = (Button) findViewById(R.id.b2);
        save = (Button) findViewById(R.id.b3);

        Intent intent = getIntent();
        String name = intent.getStringExtra("uname");
        uname.setText(name);

        ContentValues values = new ContentValues();
        cursor = db.query(dbHelper.TB_Name,null,null,null,null,null,"uid ASC");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()){
            if(name.trim().equals(cursor.getString(1))){
                pwd.setText(cursor.getString(2));
                age.setText(cursor.getString(3));
            }
            cursor.moveToNext();
        }

        showPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i==0){
                    pwd.setInputType(128);
                    showPwd.setText("隐藏密码");
                    i = 1;
                }else{
                    pwd.setInputType(129);
                    showPwd.setText("显示密码");
                    i = 0;
                }
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uname.setFocusableInTouchMode(true);
                age.setFocusableInTouchMode(true);
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uname.setFocusable(false);
                age.setFocusable(false);
            }
        });
    }
}
